package C051;

/**
 * ClassName: C01
 * Package: C051
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/2/28 21:49
 * @Version 1.0
 * @Since 1.0
 */
public class C01 {
    public static void main(String[] args) {
        int[] arr = {30,11,23,4,20};

        System.out.println(minEatingSpeed(arr, 6));
    }

    public static int minEatingSpeed(int[] piles, int h) {
        // 找出最大值
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < piles.length; i++) {
            max = Math.max(max, piles[i]);
        }

        int l = 1, r = max, mid = 0;
        // 二分
        int ans = -1;
        while (l <= r) {
            mid = l + ((r - l) >> 1);
            if(hours(piles, mid) > h) {
                l = mid + 1;
            } else {
                ans = mid;
                r = mid - 1;
            }
        }
        return ans;
    }

    private static long hours(int[] piles, int mid) {
        long ans = 0;
        for (int i = 0; i < piles.length; i++) {
            ans += (piles[i] + mid - 1) / mid;
        }
        return ans;
    }
}
